package net.hn.hnms.biz.material.service.impl;

import cn.hutool.core.date.DateUtil;
import net.hn.hnms.biz.material.mapper.StatisticMapper;
import net.hn.hnms.biz.material.service.IStatisticService;
import net.hn.hnms.biz.material.vo.MaterialCostVO;
import net.hn.hnms.biz.material.vo.StatisticVO;
import net.hn.hnms.biz.material.vo.StockInVO;
import net.hn.hnms.biz.material.vo.StockInfoVO;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ly
 * 统计服务层
 * @date 2022-08-26
 */
@Service
public class StatisticServiceImpl implements IStatisticService {


    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * 经营概览-物资统计
     *
     * @return StatisticVO
     */
    @Override
    public StatisticVO total(String mineCode, Integer limit) {
        Date[] dateRange = DateUtils.getDateRange(null, null);
        StatisticVO vo = statisticMapper.materialPlanStatistic(mineCode,DateUtil.year(dateRange[0]), dateRange[0], dateRange[1]);
        //BigDecimal yearPriceAmount = vo.getYearPriceAmount();
        //vo.setYearPriceAmount(yearPriceAmount.divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
        List<StockInVO> statisticStockIn = statisticMapper.statisticStockIn(mineCode,dateRange[0], dateRange[1]);


//        vo.setYearPriceAmount(vo.getYearPriceAmount().divide(Constants.TEN_THOUSAND, 2, RoundingMode.CEILING));
//        vo.setAdjustPriceAmount(vo.getAdjustPriceAmount().divide(Constants.TEN_THOUSAND, 2, RoundingMode.CEILING));

        //季度总数和金额 改为统计入库总数和金额
        vo.setQuarterTotal(statisticStockIn.stream().mapToInt(o->o.getQuantity()).sum());
        vo.setQuarterPriceAmount(BigDecimal.valueOf(statisticStockIn.stream().mapToDouble(o -> o.getAmount().doubleValue()).sum()).divide(Constants.TEN_THOUSAND,2, RoundingMode.CEILING));

        List<StockInfoVO> stockInfoVOS = statisticMapper.statisticStockInfo(mineCode,dateRange[0], dateRange[1]);
        List<StockInfoVO> records = new ArrayList<>();
        StockInfoVO other = new StockInfoVO();
        for (int i = 0; i < stockInfoVOS.size(); i++) {
            if (i < limit) {
                records.add(stockInfoVOS.get(i));
            }else{
                other.setQuantity(other.getQuantity() == null ? 0 : other.getQuantity());
                other.setQuantity(other.getQuantity() + stockInfoVOS.get(i).getQuantity());
            }
        }
        if (stockInfoVOS.size() > limit) {
            other.setMaterialName("其它物资");
            records.add(other);
        }
        vo.setRecords(records);
        vo.setMaterialTotal(stockInfoVOS.stream().filter(o -> o.getQuantity() != null).mapToLong(StockInfoVO::getQuantity).sum());
        return vo;
    }

    /**
     * 生产成本-物资成本
     *
     * @return MaterialCostVO
     */
    @Override
    public List<MaterialCostVO> materialCost(String mineCode, Integer year) {
        Date[] dateRange = DateUtils.getDateRangeUtilNow(year, null);
        List<MaterialCostVO> sumStock = statisticMapper.materialCost(mineCode,dateRange[0], dateRange[1]);

        List<MaterialCostVO> result = new ArrayList<>();
        for (int i = 1; i < DateUtil.month(dateRange[1])+2; i++) {
            int finalI = i;
            MaterialCostVO vo = sumStock.stream().filter(o -> o.getMonth() != null
                    && o.getMonth() == finalI).findFirst().orElse(new MaterialCostVO(i));
            result.add(vo);
        }
        return result;
    }
}
